<?php

namespace frontend\modules\home\controllers;

use common\components\Tools;
use yii\web\Controller;
use common\components\MobileDetect;
use backend\modules\home\models\Booklist;
use yii\myhelper\Response;
use yii\data\Pagination;
use Yii;

/**
 * Default controller for the `home` module
 */
class DefaultController extends Controller
{
    /**
     * Renders the index view for the module
     * @return string
     */
    public $enableCsrfValidation = false;
    public function actionIndex()
    {
        $detect = new MobileDetect();
        if($detect->isMobile()){
            return $this->redirect('/touch/default/index');
        }
        $query = Booklist::find()->where(['status' => 1,'parent_id' => 0]);
        $tab = Yii::$app->request->get('tab');
        if($tab == 'like'){
            $tab = 'like';
            $query->orderBy([
                'zan_num' => SORT_DESC,
                'click_num' => SORT_DESC,
            ]);
        }else{
            $tab = 'num';
            $query->orderBy([
                'click_num' => SORT_DESC,
                'zan_num' => SORT_DESC,
            ]);
        }
        $total_count = $query->count();
        $pages = new Pagination(['totalCount' =>$total_count, 'pageSize' => '10']);
        $list = $query->offset($pages->offset)->limit($pages->limit)->asArray()->all();
        $this->layout = false;
        return $this->render('index',[
            'tab' => $tab,
            'list' => $list,
            'pages' => $pages,
        ]);
    }

    public function actionDownload(){
        $this->layout = false;
        return $this->render('download',[
        ]);
    }

    public function actionBookItem($id){
        $this->layout = false;
        $model = $this->check($id);
        if(!$model){
            return $this->redirect('/touch/error?msgid=201');exit;
        }
        // 增加阅读量
        $model->click_num = $model->click_num + 1;
        $model->save();

        $list = array();
        if($model->type == 2){
            $list = Booklist::find()
                ->where([
                    'status' => 1,
                    'parent_id' => $model->id,
                ])
                ->select('number,front_title')
                ->orderBy([
                    'sort' => SORT_DESC,
                    'create_at' => SORT_ASC,
                ])
                ->asArray()
                ->all();
        }

        $like = $this->getRandomlist();

        return $this->render('book-item',[
            'model' => $model,
            'list' => $list,
            'like' => $like,
        ]);
    }

    public function getRandomlist(){
        $idlist = Booklist::find()
            ->where(['status' => 1, 'parent_id' => 0])
            ->select('id')
            ->asArray()
            ->all();
        foreach($idlist as $v){
            $ids[] = $v['id'];
        }
        $rand_key = array_rand($ids,6);
        foreach($rand_key as $v){
            $numbers[] = $ids[$v];
        }
        $list = Booklist::find()
            ->select('id,number,front_title,thumb_img')
            ->where(['status' => 1,'parent_id' => 0])
            ->andWhere(['in','id',$numbers])
            ->orderBy([
                'sort' => SORT_DESC,
                'click_num' => SORT_DESC,
                'update_at' => SORT_DESC,
            ])
            ->asArray()
            ->all();
        return $list;

    }

    public function check($id,$colume = 'number'){
        if($colume == 'id'){
            $where = [
                'status' => 1,
                'id' => $id,
            ];
        }else{
            $where = [
                'status' => 1,
                'number' => $id,
            ];
        }
        $model = Booklist::find()
            ->where($where)->one();
        return $model;

    }

    public function actionSitemap(){
        $this->layout = false;
        $list = Booklist::find()
            ->where(['status' => 1, 'parent_id' => 0])
            ->select('id,front_title,number')
            ->asArray()
            ->all();
        return $this->render('sitemap',[
            'list' => $list,
        ]);
    }

}
